<?php


namespace App\Admin\Services\Wechat;


use App\Repositories\Wechat\MiniProgramOperationRecordRepository;

class MiniProgramOperationRecordService {
    private $_repository;

    public function __construct(MiniProgramOperationRecordRepository $repository) {
        $this->_repository = $repository;
    }

    /**
     * 小程序操作记录列表
     *
     * @return mixed
     */
    public function page() {
        $operationRecordRes = $this->_repository->queryByConditionAndPage(
            function ($query) {
                $query->with([
                    'user'                => function ($userSubQuery) {
                        $userSubQuery->select(['id', 'name']);
                    },
                    'appProject'          => function ($appProjectSubQuery) {
                        $appProjectSubQuery->select(['id', 'name']);
                    },
                    'miniProgramTemplate' => function ($miniProgramTemplateSubQuery) {
                        $miniProgramTemplateSubQuery->select(['id', 'name']);
                    },
                ]);
            }
        );
    
        $spacer = ' ✏№ ';
        foreach ($operationRecordRes['list'] as $key => &$val) {
            $operationRecordRes['list'][$key]['user_name']                  = $val->user ? "{$val->user->name}{$spacer}{$val->user->id}" : '';
            $operationRecordRes['list'][$key]['app_project_name']           = $val->appProject ? "{$val->appProject->name}{$spacer}{$val->appProject->id}" : '';
            $operationRecordRes['list'][$key]['mini_program_template_name'] = $val->miniProgramTemplate ? "{$val->miniProgramTemplate->name}{$spacer}{$val->miniProgramTemplate->id}" : '';
        }
        return $operationRecordRes;
    }
}